package com.zryc.shop.pms.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.pms.entity.ProductAttributeCategory;
import com.zryc.shop.pms.service.IProductAttributeCategoryService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/pms/productAttribute/category")
@Api(tags = "产品属性分类管理接口列表")
public class ProductAttributeCategoryController {
    @Autowired
    private IProductAttributeCategoryService proAttrCateService;

    @GetMapping("/list")
    @ApiOperation("分页查询列表")
    public ResponseResult list(@RequestParam(defaultValue = "1") @ApiParam(value = "页码", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示条数", required = true) Integer pageSize) {
        Page page = new Page(pageNum, pageSize);
        proAttrCateService.page(page);
        return new ResponseResult(200, "查询完成", page);
    }

    @PostMapping("/create")
    @ApiOperation("创建属性分类")
    public ResponseResult create(@RequestParam @ApiParam(value = "属性分类名称", required = true) String name) {
        ProductAttributeCategory category = new ProductAttributeCategory();
        category.setName(name);
        category.setAttributeCount(0);
        category.setParamCount(0);
        proAttrCateService.save(category);
        return new ResponseResult(200, "创建成功");
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除属性分类")
    public ResponseResult delete(@PathVariable @ApiParam(value = "属性分类ID", required = true) Long id) {
        proAttrCateService.removeById(id);
        return new ResponseResult(200, "删除成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改属性分类名称")
    public ResponseResult create(@PathVariable @ApiParam(value = "属性分类ID", required = true) Long id,
                                 @RequestParam @ApiParam(value = "属性分类名称", required = true) String name) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("name", name);
        proAttrCateService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }
}
